package com.content.management.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.content.management.dao.DictionaryDataDao;
import com.content.management.domain.entity.DictionaryData;
import com.content.management.service.DictionaryDataService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 字段数据（主键id能用于业务） 服务实现类
 * </p>
 *
 * @author xinghu
 * @since 2020-04-04
 */
@Service
public class DictionaryDataServiceImpl extends ServiceImpl<DictionaryDataDao, DictionaryData> implements DictionaryDataService {

	@Override
	public Map<String, DictionaryData> getMapByDirectoryFlag(String directoryFlag) {
		return this.lambdaQuery().eq(DictionaryData::getDirectoryFlag, directoryFlag).list().stream()
				.collect(Collectors.toMap(DictionaryData::getDictionaryFlag, Function.identity()));
	}

	@Override
	public boolean saveOrUpdate(DictionaryData entity) {
		DictionaryData last = this.lambdaQuery().last(" limit 1").orderByDesc(DictionaryData::getId).one();
		if (Objects.nonNull(last)) {
			entity.setDictionaryFlag(entity.getDirectoryFlag() + last.getId());
		} else {
			entity.setDictionaryFlag(entity.getDirectoryFlag() + 0);
		}
		return super.saveOrUpdate(entity);
	}
}
